<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简化受控组件</title>
</head>
<body>
    <div id="app"></div>

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    
    <script type="text/babel">
        class Demo extends React.Component {
            // 初始化state
            state = {
                username:'1',
                password:'2'
            }

            // 自定义事件
            saveFormData = (dateType)=>{
                return (event) => {
                    this.setState({[dateType]:event.target.value})
                }
            }

            handleSubmit = (event) => {
                event.preventDefault() //阻止表单默认提交
                const {username,password} = this.state
                alert(`${username},${password}`)
            }

            render() {
                return (
                    <form action="" onSubmit={this.handleSubmit}>
                        用户名：<input onChange = {this.saveFormData('username')} type="text"/>
                        密码：  <input onChange = {this.saveFormData('password')} type="password"/>
                        <button>登录</button>
                    </form>
                )
            }
            
        }

        // 渲染组件
        ReactDOM.render(<Demo/>,document.querySelector('#app'))
    </script>
</body>
</html>